package com.mysmartphone.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mysmartphone.tools.Tools;
import com.mysmartphone.vistas.R;
import com.mysmartphone.vo.Phone;

public class PhoneListAdapter extends BaseAdapter implements OnClickListener {

	private Context context;
	private ArrayList<Phone> listPhone;
	private Toast toast;
	private long time_start =0;
	private long time_end =0;

	public PhoneListAdapter(Context context, ArrayList<Phone> listPhone) {
		this.context = context;
		this.listPhone = listPhone;
		
	}

	@Override
	public int getCount() {
		return listPhone.size();
	}

	@Override
	public Object getItem(int position) {
		return listPhone.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater) context
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.main_list_row, null);

		}
		ImageView logo = (ImageView) convertView.findViewById(R.id.logo);
		TextView txtBrandModel = (TextView) convertView.findViewById(R.id.brandModel);
		TextView txtCore = (TextView) convertView.findViewById(R.id.core);
		ImageView image = (ImageView) convertView.findViewById(R.id.image);
		TextView txtRamRom = (TextView) convertView.findViewById(R.id.ramRom);
		TextView txtScreenSize = (TextView) convertView.findViewById(R.id.screenSize);
		//CheckBox check = (CheckBox) convertView.findViewById(R.id.check);
		TextView txtOperatingSystem = (TextView) convertView.findViewById(R.id.operatingSystem);
		
		Phone phone = (Phone) getItem(position);

		image.setImageBitmap(phone.getImage());
		logo.setImageResource(phone.getLogo());
		txtBrandModel.setText(createText(phone,0));
		txtCore.setText(createText(phone,1));
		txtRamRom.setText(createText(phone,2));
		txtScreenSize.setText(createText(phone, 3));
		txtOperatingSystem.setText(createText(phone, 4));
		//check.setChecked(phone.isCheck());
		//check.setTag(phone);
		//check.setOnClickListener(this);

		return convertView;
	}

	@Override
	public void onClick(View view) {
		Phone phone = (Phone) view.getTag();
		int con = 0;
		CheckBox ch = (CheckBox) view;
		
		for (int i=0;i<listPhone.size();i++) {
			Phone auxPhone =listPhone.get(i);
			if (ch.isChecked()) {				
				if(auxPhone.getIdPhone() == phone.getIdPhone()) {
					
					auxPhone.setCheck(true);
					listPhone.set(i,auxPhone);
				}
			}
			if(auxPhone.isCheck()){
				con++;
			}
		}
		if (con > 3) {
			if(ch.isChecked()){			
				time_start = System.currentTimeMillis();
				if(Tools.timeMessage(time_start,time_end,3000l)){
					toast = Toast.makeText(context, R.string.phoneCheckThree, Toast.LENGTH_SHORT);
					toast.show();
					time_end = System.currentTimeMillis();
				}
			}
			ch.setChecked(false);
		} 
		else {
			phone.setCheck(((CheckBox) view).isChecked());
		}

	}
	private String createText(Phone phone,int value){
		
		String cad ="";
		
		switch (value) {
		case 0:
			if(phone.getBrand() !=null)	cad =phone.getBrand();
			if(phone.getModel() !=null)	cad +=" "+phone.getModel();
			break;
		case 1:
			cad = this.context.getString(R.string.core)+": ";
			if(phone.getNameCore() != null)	cad += phone.getNameCore() +" ";
			if(phone.getSpeedCore() >0) cad += phone.getSpeedCore()+phone.getSpeedDesCore()+" ";
			//if(phone.getNumberCore() != 0)cad += phone.getNumberCore()+" ";
			if(phone.getNumberDesCore() != null)cad += phone.getNumberDesCore();
			break;
		case 2:
			cad = context.getString(R.string.memory)+": ";
			if(phone.getMemoryROM()>0){
				cad+=phone.getMemoryROM();
				if(phone.getMemoryDesRom()!=null)cad+=phone.getMemoryDesRom() +" "+context.getString(R.string.rom);
			}
			if(phone.getMemoryRAM()>0){
				cad+=" "+phone.getMemoryRAM();
				if(phone.getMemoryDesRam()!=null)cad+=phone.getMemoryDesRam()+" "+context.getString(R.string.ram);
			}
			break;
		case 3:
			cad = context.getString(R.string.screen)+": "; 
			if(phone.getScreenSize()>0)	cad += phone.getScreenSize()+ " "+context.getString(R.string.inches);			
			break;
		case 4:
			cad = context.getString(R.string.operatingSystem)+": ";
			if(phone.getOperatingSystem()!=null)cad += phone.getOperatingSystem();	
			break;
		}
		return cad;

	}
}
